//
//  JparkViewController.m
//  Park
//
//  Created by 123456 on 2017/11/7.
//  Copyright © 2017年 YTYangK. All rights reserved.
//

#import "JparkViewController.h"
#import "TopView.h"
#import "JparkTableViewCell.h"
#import "MJRefresh.h"
#import "JWPlayer.h"
#import "JPUSH.h"

@interface JparkViewController ()<UITableViewDataSource,UITableViewDelegate>
{
    
    TopView *Tview;
    MJRefreshNormalHeader* header;
    int _page;
    BOOL one;
    JWPlayer *player;
    
    int T;//头部高度变量
    int Scontent;//scrollview内容高度变量
    int Sexternal;//scrollview外部高度变量
    int Notice;//公告变量

}

@property (nonatomic, strong) UIPageControl *pageControl; //!< pageControl属性;
@property (strong, nonatomic) IBOutlet NSLayoutConstraint *JptableviewTop;


@end

@implementation JparkViewController


- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    
    [[JPUSH sharedJPUSH] albbBindChannel]; //这个是绑定推送的账号，如果注册失败会重复执行

    [self.navigationController setNavigationBarHidden:YES animated:animated];
    if (player.End ==YES) {
        [player play];
    }

}

- (void)viewWillDisappear:(BOOL)animated {
    [super viewWillDisappear:animated];
    
    [self.navigationController setNavigationBarHidden:NO animated:animated];
    
    if (player.End ==YES) {
        [player pause];
    }
    
//    dispatch_source_cancel(Tview.marqueeView.timer);

}


- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    [self Layout];
    
    self.JparkTableView.separatorStyle = UITableViewCellSeparatorStyleNone;

    
    //头部
    Tview = [[NSBundle mainBundle]loadNibNamed:@"TopView" owner:self options:nil][0];
    Tview.frame = CGRectMake(0,0,self.view.size.width,T);
    self.JparkTableView.tableHeaderView = Tview;
    

    
    
//    MJ刷新
    [self refreshTableView];
    
    NSString *path = [[NSBundle mainBundle]pathForResource:@"大楼视频" ofType:@"mp4"];

    player=[[JWPlayer alloc]initWithFrame:CGRectMake(0, 0, Tview.TScrollview.width, Sexternal)];
//    [player updatePlayerWith:[NSURL URLWithString:@"http://baobab.wdjcdn.com/14564977406580.mp4"]];
    NSURL *url = [NSURL fileURLWithPath:path];
    [player updatePlayerWith:url];

    
    self.pageControl = [[UIPageControl alloc] initWithFrame:CGRectMake(50, Scontent - 15, Tview.size.width-100, 15)];
    // 设置背景颜色:                          // 背景颜色的透明度

    //设置pageControl的总的点数:
    self.pageControl.numberOfPages = 5;
    // 设置pageControl当前的点序号: (从0开始)
    self.pageControl.currentPage = 0;
    // 设置点的颜色:
    self.pageControl.pageIndicatorTintColor = [UIColor grayColor];
    // 设置当前点的颜色:
    self.pageControl.currentPageIndicatorTintColor = [UIColor orangeColor];
    // 添加目标事件: (UIControlEventValueChanged)
    [self.pageControl addTarget:self action:@selector(clickPageControl:) forControlEvents:UIControlEventValueChanged];
    [Tview addSubview:self.pageControl];
    
    [player showInSuperView:Tview.TScrollview andSuperVC:self];

    for (int i =1; i<=5; i++)
    {
        UIImageView *imageView= [[UIImageView alloc] initWithFrame:CGRectMake(i * SCREEN_WIDTH,0,SCREEN_WIDTH,Scontent)];
        [imageView setImage:[UIImage imageNamed:[NSString stringWithFormat:@"资源 %d",i]]];
        [Tview.TScrollview addSubview:imageView];
    }
    
    Tview.TScrollview.contentSize = CGSizeMake( self.view.bounds.size.width*5,Scontent);
    Tview.TScrollview.pagingEnabled = YES;
    Tview.TScrollview.showsHorizontalScrollIndicator =NO;
    Tview.TScrollview.bounces = NO;
    Tview.TScrollview.delegate = self;

    
    
    self.simpleMarqueeView = [[UUMarqueeView alloc] initWithFrame:CGRectMake(5, 0,Tview.NoticeView.width-Notice,22)];
    _simpleMarqueeView.delegate = self;
    _simpleMarqueeView.timeIntervalPerScroll = 2.0f;
    _simpleMarqueeView.timeDurationPerScroll = 1.0f;
    _simpleMarqueeView.touchEnabled = YES;
    
    [Tview.NoticeView addSubview:_simpleMarqueeView];
    [_simpleMarqueeView reloadData];
    
    [_simpleMarqueeView start];

    
    
}


//跑马灯行数
- (NSUInteger)numberOfVisibleItemsForMarqueeView:(UUMarqueeView*)marqueeView {
    return 1;
    
}

//跑马灯数据
- (NSArray*)dataSourceArrayForMarqueeView:(UUMarqueeView*)marqueeView {
    
    self.simpleMarqueeViewData = @[@"祝贺永顺餐厅1月13日正式营业，欢迎光临！",
                                   @"祝贺志美酒店1月13日正式营业，欢迎光临！",
                                   @"专车预约正式启动使用，欢迎各位下单预约！"];
    
    return _simpleMarqueeViewData;
}
//跑马灯内容布局
- (void)createItemView:(UIView*)itemView forMarqueeView:(UUMarqueeView*)marqueeView {
    itemView.backgroundColor = [UIColor whiteColor];
    UILabel *content = [[UILabel alloc] initWithFrame:itemView.bounds];
    content.font = [UIFont systemFontOfSize:13.0f];
    content.tag = 1001;
    [itemView addSubview:content];
}

//添加跑马灯数据
- (void)updateItemView:(UIView*)itemView withData:(id)data forMarqueeView:(UUMarqueeView*)marqueeView {
    // for simpleMarqueeView
    UILabel *content = [itemView viewWithTag:1001];
    content.text = data;
}

//点击跑马灯回调
- (void)didTouchItemViewAtIndex:(NSUInteger)index forMarqueeView:(UUMarqueeView*)marqueeView {
    NSLog(@"Touch at index %lu - \"%@\"", (unsigned long)index, _simpleMarqueeViewData[index]);
}

// pageControl的响应方法:
- (void)clickPageControl:(UIPageControl *)pageControl
{
    //NSLog(@"%ld", pageControl.currentPage); //!< 打印当前点序号;
    
    if (pageControl.currentPage > 0) {
        [player pause];

    }else{
        
        [player play];

    }
    
    [Tview.TScrollview setContentOffset:CGPointMake(pageControl.currentPage * self.view.size.width, 0) animated:YES];
}

//scrollView的代理方法:
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView
{
    
    NSLog(@"%f",scrollView.contentOffset.x );
    
    if (scrollView.contentOffset.x > 0) {
        [player pause];
    
    }else{
        
        [player play];

    }
    if (scrollView.contentOffset.x >= 0) {
    // 计算偏移量:
    self.pageControl.currentPage = scrollView.contentOffset.x / 375;
    }
    
   
}


- (void)refreshTableView {
    
    header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
        _page = 1;
        [self RequestData:_page];
        
        [header endRefreshing];
        
    }];
    header.automaticallyChangeAlpha = YES;
    header.lastUpdatedTimeLabel.hidden = YES;
//    [header beginRefreshing];
    self.JparkTableView.header = header;
    

}




-(void)RequestData:(int)page{
    
    
    
}


#pragma mark -  TableView DataSource
- (NSInteger)numberOfSectionsInTableView:(UITableView*)tableView {
    return 0;
}

- (NSInteger)tableView:(UITableView*)tableView numberOfRowsInSection:(NSInteger)section {
    return 0;
}

- (CGFloat)tableView:(UITableView*)tableView heightForRowAtIndexPath:(NSIndexPath*)indexPath {
    return 100;
}


- (UITableViewCell*)tableView:(UITableView*)tableView cellForRowAtIndexPath:(NSIndexPath*)indexPath {

    JparkTableViewCell *cell  = [tableView dequeueReusableCellWithIdentifier:@"cell"];
    cell.selectionStyle = UITableViewCellSelectionStyleNone;


    return cell;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{



}


- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    return 65;
}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{

    UIImageView *image  =[[UIImageView alloc]initWithFrame:CGRectMake(0, 0, self.view.size.width, self.view.size.height)];
    image.image = [UIImage imageNamed:@"12"];
    
    return image;
}


-(void)Layout
{
   
    if(SYSTEM_VERSION_EQUAL_TO_W_orH(SCREEN_HEIGHT, @"568.0")){
  
//        T = 940;
//        Scontent = 280;
//        Sexternal = 280;

        Notice = 58;
        
        T = 860;
        Scontent = 280;
        Sexternal = 280;
        
    }else if (SYSTEM_VERSION_EQUAL_TO_W_orH(SCREEN_HEIGHT, @"667.0")){

//        T = 850;
//        Scontent = 289;
//        Sexternal = 290;
        
        T = 760;
        Scontent = 289;
        Sexternal = 290;
        
    }else if (SYSTEM_VERSION_EQUAL_TO_W_orH(SCREEN_HEIGHT, @"736.0")){
   
//        T = 800;
//        Scontent = 289;
//        Sexternal = 290;
        
        T = 710;
        Scontent = 289;
        Sexternal = 290;

        
    }else if (SYSTEM_VERSION_EQUAL_TO_W_orH(SCREEN_HEIGHT, @"812.0")){
        self.JptableviewTop.constant = -44;
//        T = 720;
//        Scontent = 289;
//        Sexternal = 300;
        
        T = 630;
        Scontent = 289;
        Sexternal = 300;
        
    }

    
    
}

@end
